#!/bin/bash

set -eux # -x for verbose logging to juju debug-log

# https://weblogs.java.net/blog/johnsmart/archive/2009/06/installing_sona.html
# http://en.wikipedia.org/wiki/Pip_(package_manager)
# https://pypi.python.org/pypi/pip

juju-log "III Getting any config parameters."

#szAcceptDataViaUdp=`config-get AcceptUdpConnections`
#szAcceptDataViaUdpDefault="True"

#if [ -z "$szAcceptDataViaUdp" ]
#then
#  szAcceptDataViaUdp="True"
#  juju-log "DDD Not set: AcceptUdpConnections, default to: $szAcceptDataViaUdp"
#else
#  # TODO make sure that it is only true or false, anything and it must got back to the default value.
#  juju-log "DDD AcceptUdpConnections: $szAcceptDataViaUdp"
#fi

##############################
# INSTALL SYSTEM DEPENDENCIES
#############################
juju-log "III Installing maven2 and jdk via apt-get"

apt-get -y install maven2 openjdk-7-jdk

wget http://dist.sonar.codehaus.org/sonar-3.6.2.zip
wget http://dist.sonar.codehaus.org/sonar-3.6.2.zip.md5
# md5sum -c sonar-3.6.2.zip.md5
# TODO verify the md5 is actually ok.

# TODO make the target dir a config var.
unzip -d /usr/local sonar-3.6.2.zip
ln -s /usr/local/sonar-3.6.2 /usr/local/sonar

# TODO V make it configurable if the module is to be installed.
juju-log "III Installing the C++ Plugin."

#################################
# INSTALL C++ PLUGIN DEPENDENCIES
#################################
juju-log "III Installing cppcheck rats valgrind gcc gcovr via apt-get"

apt-get -y install cppcheck rats valgrind gcc
# gcovr (only in 13.04+)

wget http://repository.codehaus.org/org/codehaus/sonar-plugins/cxx/sonar-cxx-plugin/0.9/sonar-cxx-plugin-0.9.jar
# TODO C make md5sum
mv sonar-cxx-plugin-0.9.jar /usr/local/sonar/extensions/plugins/

juju-log "III Installing the javascript Plugin."

########################################
# INSTALL javascript PLUGIN DEPENDENCIES
########################################
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/javascript/sonar-javascript-plugin/1.3/sonar-javascript-plugin-1.3.jar
# TODO C make md5sum
mv sonar-javascript-plugin-1.3.jar /usr/local/sonar/extensions/plugins/


juju-log "III Installing the python Plugin."
####################################
# INSTALL python PLUGIN DEPENDENCIES
####################################
apt-get -y install pylint
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/python/sonar-python-plugin/1.1/sonar-python-plugin-1.1.jar
# TODO C make md5sum
mv sonar-python-plugin-1.1.jar /usr/local/sonar/extensions/plugins/

juju-log "III Installing the Quality Index Plugin"
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-quality-index-plugin/1.1.3/sonar-quality-index-plugin-1.1.3.jar
mv sonar-quality-index-plugin-1.1.3.jar /usr/local/sonar/extensions/plugins/

juju-log "III Installing the SIG Maintainability Model Plugin"
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-sigmm-plugin/1.0.1/sonar-sigmm-plugin-1.0.1.jar
mv sonar-sigmm-plugin-1.0.1.jar /usr/local/sonar/extensions/plugins/

juju-log "III Installing the Technical Debt Plugin"
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-technicaldebt-plugin/1.2.1/sonar-technicaldebt-plugin-1.2.1.jar
mv sonar-technicaldebt-plugin-1.2.1.jar /usr/local/sonar/extensions/plugins/

juju-log "III Installing the Toxicity Chart Plugin"
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/toxicity-chart/sonar-toxicity-chart-plugin/0.1/sonar-toxicity-chart-plugin-0.1.jar
mv sonar-toxicity-chart-plugin-0.1.jar /usr/local/sonar/extensions/plugins/


juju-log "III Installing the PDF Plugin"
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/pdf-report/sonar-pdfreport-plugin/1.3.1/sonar-pdfreport-plugin-1.3.1.jar
mv sonar-pdfreport-plugin-1.3.1.jar /usr/local/sonar/extensions/plugins/


juju-log "III Installing the Abacus Plugin"
wget http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-abacus-plugin/0.1/sonar-abacus-plugin-0.1.jar
mv sonar-abacus-plugin-0.1.jar /usr/local/sonar/extensions/plugins/


